package crm.view.main;

import crm.view.Admin.*;
import crm.view.LoginPage;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.time.LocalTime;

public class AdminMain implements ActionListener, Runnable {
    JFrame frame = null;
    JMenuBar menuBar = null;
    JMenu manageMenu = null;
    JMenu checkMenu = null;
    JMenu lockMenu = null;
    JMenu time = null;

    JMenuItem stop = null;
    JMenuItem over = null;

    JPanel panel = null;
    JMenuItem add = null;
    JMenuItem delete = null;
    JMenuItem update = null;
    JMenuItem updatePro = null;

    JMenuItem customerInfo = null;
    JMenuItem products = null;
    JMenuItem count = null;
    JMenuItem lock = null;

    JTextField text;
    Timer time2;

    JMenu feedback = null;
    JMenuItem feedbackMenu = null;

    public static void main(String[] args) {
        AdminMain adminMain = new AdminMain();
        new Thread(adminMain).start();
    }

    public AdminMain() {
        init();
        frame.setBounds(680, 300, 1096, 776);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        frame.setJMenuBar(menuBar);
        frame.setVisible(true);
    }

    public void init() {
        Font font1 = new Font("宋体", Font.BOLD, 30);
        frame = new JFrame("welcome,系统管理员!");
        panel = new JPanel();
        menuBar = new JMenuBar();
        menuBar.setBounds(0, 0, 30, 100);
        manageMenu = new JMenu("客户管理");
        checkMenu = new JMenu("查询统计");
        time = new JMenu("计时功能");
        lockMenu = new JMenu("锁屏");

        feedback = new JMenu("反馈信息");
        feedbackMenu = new JMenuItem("查看反馈");


        time2 = new Timer(1000, this); //WindowTime对象做计时器的监视器
        text = new JTextField(10);
        text.setBounds(800, 50, 200, 50);
        text.setEditable(false);
        frame.add(text);

        manageMenu.setFont(font1);
        checkMenu.setFont(font1);
        lockMenu.setFont(font1);
        time.setFont(font1);


        stop = new JMenuItem("暂停");
        over = new JMenuItem("继续");
        time.add(stop);
        time.addSeparator();
        time.add(over);

        ImageIcon icon1 = new ImageIcon("src/img/lock50.png");
        lockMenu.setIcon(icon1);
        ImageIcon icon2 = new ImageIcon("src/img/管理 (2).png");
        manageMenu.setIcon(icon2);
        ImageIcon icon3 = new ImageIcon("src/img/查询.png");
        checkMenu.setIcon(icon3);
        ImageIcon icon4 = new ImageIcon("src/img/计时.png");
        time.setIcon(icon4);

        ImageIcon icon11 = new ImageIcon("src/img/反馈.png");
        feedback.setIcon(icon11);
        feedbackMenu.setIcon(icon11);

        ImageIcon icon8 = new ImageIcon("src/img/暂停.png");
        stop.setIcon(icon8);
        ImageIcon icon9 = new ImageIcon("src/img/结束.png");
        over.setIcon(icon9);

        menuBar.add(manageMenu);
        menuBar.add(checkMenu);
        menuBar.add(time);
        menuBar.add(feedback);
        feedback.add(feedbackMenu);
        menuBar.add(lockMenu);


        add = new JMenuItem("添加客户");
        delete = new JMenuItem("删除客户");
        update = new JMenuItem("修改客户信息");
        updatePro = new JMenuItem("修改客户产品信息");

        manageMenu.add(add);
        manageMenu.addSeparator();       // 添加一条分割线
        manageMenu.add(delete);
        manageMenu.addSeparator();       // 添加一条分割线
        manageMenu.add(updatePro);
        manageMenu.addSeparator();       // 添加一条分割线
        manageMenu.add(update);

        customerInfo = new JMenuItem("查询客户基本信息");
        products = new JMenuItem("查询客户所购产品");
        count = new JMenuItem("统计员工业绩信息");
        checkMenu.add(customerInfo);
        checkMenu.addSeparator();       // 添加一条分割线
        checkMenu.add(products);
        checkMenu.addSeparator();       // 添加一条分割线
        checkMenu.add(count);



        lock = new JMenuItem("开始锁屏");
        lockMenu.add(lock);



        ImageIcon icon = new ImageIcon("src/img/2333.jpg");
        JLabel imgLabel = new JLabel(icon);
        imgLabel.setBounds(0, 0, icon.getIconWidth(), icon.getIconHeight());
        panel.add(imgLabel);
        frame.add(panel);

        Font font2 = new Font("宋体", Font.BOLD, 25);
        add.setFont(font2);
        delete.setFont(font2);
        update.setFont(font2);
        updatePro.setFont(font2);
        text.setFont(font2);

        customerInfo.setFont(font2);
        products.setFont(font2);
        count.setFont(font2);

        stop.setFont(font2);
        over.setFont(font2);
        feedback.setFont(font2);
        feedbackMenu.setFont(font2);
        lock.setFont(font2);

        add.addActionListener(this);
        delete.addActionListener(this);
        update.addActionListener(this);
        updatePro.addActionListener(this);

        customerInfo.addActionListener(this);
        products.addActionListener(this);
        count.addActionListener(this);

        stop.addActionListener(this);
        over.addActionListener(this);

        lock.addActionListener(this);
        feedbackMenu.addActionListener(this);

    }


    @Override
    public void run() {
        time2.start();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == time2) {
            LocalTime dateTime = LocalTime.now();
            String timeStr =
                    dateTime.getHour() + ":" + dateTime.getMinute() + ":" + dateTime.getSecond();
            text.setText(timeStr);
        } else if ("暂停".equals(e.getActionCommand()))
            time2.stop();
        else if ("继续".equals(e.getActionCommand()))
            time2.restart();

        else if ("添加客户".equals(e.getActionCommand())) {
            new addCustomer();
        } else if ("删除客户".equals(e.getActionCommand())) {
            new deleteCustomer();
        } else if ("修改客户信息".equals(e.getActionCommand())) {
            new UpdateCustomer();
        } else if ("修改客户产品信息".equals(e.getActionCommand())) {
            new UpdatePro();
        } else if ("查询客户基本信息".equals(e.getActionCommand())) {
            new Query();
        } else if ("查询客户所购产品".equals(e.getActionCommand())) {
            new Query2();
        } else if ("统计员工业绩信息".equals(e.getActionCommand())) {
            new QueryPerformance();
        } else if ("开始锁屏".equals(e.getActionCommand())) {
            frame.dispose();
            new LoginPage();
//            try {
//                Runtime.getRuntime().exec("RunDll32.exe user32.dll,LockWorkStation");
//            } catch (IOException ioException) {
//                ioException.printStackTrace();
//            }

        } else if ("查看反馈".equals(e.getActionCommand())) {
            new lookFeedBack();
        }

    }
}
